package com.nineclock.doc.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.threadlocal.CurrentUserHolder;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.doc.dto.DocFileDto;
import com.nineclock.doc.dto.DocFolderDto;
import com.nineclock.doc.dto.DocumentDto;
import com.nineclock.doc.mapper.DocCollaborationsMapper;
import com.nineclock.doc.mapper.DocFileMapper;
import com.nineclock.doc.mapper.DocFolderMapper;
import com.nineclock.doc.pojo.DocCollaborations;
import com.nineclock.doc.pojo.DocFile;
import com.nineclock.doc.pojo.DocFolder;
import com.nineclock.doc.service.DocFolderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author ：zhouc
 * @date ：Created in 2021/6/28
 * @description ：
 * @version: 1.0
 */
@Service
public class DocFolderServiceImpl implements DocFolderService {
    @Autowired
    DocFolderMapper docFolderMapper;
    @Autowired
    DocFileMapper docFileMapper;
    @Autowired
    DocCollaborationsMapper docCollaborationsMapper;
    @Override
    public DocumentDto folderFileList( Long parentFoldId,String keyword) {
    
        //企业Id
        Long companyId = CurrentUserHolder.get().getCompanyId();
        //企业员工Id
        Long companyUserId = CurrentUserHolder.get().getCompanyUserId();
        if (keyword != "") {
            LambdaQueryWrapper<DocFolder> docFolderLambdaQueryWrapper = new LambdaQueryWrapper<>();
            docFolderLambdaQueryWrapper.eq(DocFolder::getCreateUserId,companyUserId);
            docFolderLambdaQueryWrapper.eq(DocFolder::getCompanyId,companyId);
            docFolderLambdaQueryWrapper.eq(DocFolder::getStatus, 1);
            docFolderLambdaQueryWrapper.like(DocFolder::getName,keyword);
            List<DocFolder> docFolders = docFolderMapper.selectList(docFolderLambdaQueryWrapper);
            DocumentDto documentDto = new DocumentDto();
            //设置文件到返回对象中
            documentDto.setFolders(BeanHelper.copyWithCollection(docFolders, DocFolderDto.class));
            LambdaQueryWrapper<DocFile> docFileLambdaQueryWrapper = new LambdaQueryWrapper<>();
            docFileLambdaQueryWrapper.eq(DocFile::getCompanyId, companyId);
            docFileLambdaQueryWrapper.eq(DocFile::getCreateUserId, companyUserId);
            docFileLambdaQueryWrapper.eq(DocFile::getStatus, 1);
            docFileLambdaQueryWrapper.like(DocFile::getName, keyword);
            List<DocFile> docFiles = docFileMapper.selectList(docFileLambdaQueryWrapper);
            documentDto.setFiles(BeanHelper.copyWithCollection(docFiles, DocFileDto.class));
            return documentDto;
        }else {
            LambdaQueryWrapper<DocFolder> docFolderLambdaQueryWrapper = new LambdaQueryWrapper<>();
            docFolderLambdaQueryWrapper.eq(DocFolder::getCreateUserId, companyUserId);
            docFolderLambdaQueryWrapper.eq(DocFolder::getParentId, parentFoldId);
            docFolderLambdaQueryWrapper.eq(DocFolder::getCompanyId, companyId);
            docFolderLambdaQueryWrapper.eq(DocFolder::getStatus, 1);
            List<DocFolder> docFolders = docFolderMapper.selectList(docFolderLambdaQueryWrapper);
            DocumentDto documentDto = new DocumentDto();
            //设置文件到返回对象中
            documentDto.setFolders(BeanHelper.copyWithCollection(docFolders, DocFolderDto.class));
            //根据当前企业用户Id查询文件Id
            LambdaQueryWrapper<DocFile> docFileLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            docFileLambdaQueryWrapper1.eq(DocFile::getCreateUserId, companyUserId);
            docFileLambdaQueryWrapper1.eq(DocFile::getCompanyId, companyId);
            List<DocFile> docFiles1 = docFileMapper.selectList(docFileLambdaQueryWrapper1);
            if (CollectionUtils.isEmpty(docFiles1)) {
                throw new RuntimeException("此用户没有创建文件");
            }
            Set<Long> collect = docFiles1.stream().map(docFile -> {
                return docFile.getId();
            }).collect(Collectors.toSet());
            //根据文件Id获取协作者的企业用户Id
            LambdaQueryWrapper<DocCollaborations> docCollaborationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            docCollaborationsLambdaQueryWrapper.in(DocCollaborations::getFileId, collect);
            List<DocCollaborations> docCollaborations = docCollaborationsMapper.selectList(docCollaborationsLambdaQueryWrapper);
            Set<Long> collect1 = docCollaborations.stream().map(docCollaborations1 -> {
                return docCollaborations1.getCollaborationId();
            }).collect(Collectors.toSet());
            collect1.add(companyUserId);
            LambdaQueryWrapper<DocFile> docFileLambdaQueryWrapper = new LambdaQueryWrapper<>();
            docFileLambdaQueryWrapper.in(DocFile::getCreateUserId, collect1);
            docFileLambdaQueryWrapper.eq(DocFile::getCompanyId, companyId);
            docFileLambdaQueryWrapper.eq(DocFile::getFolderId, parentFoldId);
            docFileLambdaQueryWrapper.eq(DocFile::getStatus, 1);
            List<DocFile> docFiles = docFileMapper.selectList(docFileLambdaQueryWrapper);
            documentDto.setFiles(BeanHelper.copyWithCollection(docFiles, DocFileDto.class));
            return documentDto;
        }
    }

    @Override
    public DocFileDto getFileByFileId(Long id) {
        DocFile docFile = docFileMapper.selectById(id);
        return BeanHelper.copyProperties(docFile, DocFileDto.class);
    }

    @Override
    public void insertFolder(DocFolderDto docFolderDto) {

        if (docFolderDto.getName() == null || docFolderDto.getParentId() == null) {
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        DocFolder docFolder = new DocFolder();
        docFolder.setCompanyId(CurrentUserHolder.get().getCompanyId());
        docFolder.setCreateTime(new DateTime());
        docFolder.setCreateUserId(CurrentUserHolder.get().getCompanyUserId());
        docFolder.setName(docFolderDto.getName());
        docFolder.setParentId(docFolderDto.getParentId());
        docFolder.setStatus((short) 1);
        docFolder.setOrderNum(0);

        docFolderMapper.insert(docFolder);
    }

}
